#import "SEGElevatorControllAlertView.h"
#define kFloorHeight kPickViewHeight
#define kFloorHeadBottom 40 * kKitScaleHeight


@implementation SEGElevatorControllAlertFloorModel
@end

@implementation SEGElevatorControllAlertViewModel
-(instancetype)init{
    if (self == [super init]) {
        _widgetWidth = [UIScreen mainScreen].bounds.size.width;
    }
    
    return self;
}
@end

/**************************SEGElevatorControllAlertViewLayout************************/
@implementation SEGElevatorControllAlertViewLayout
- (instancetype)initWithData:(SEGElevatorControllAlertViewModel *)data type:(SEGEnumElevatorControllAlertViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self segElevatorControllAlertViewLayout];
    return self;
}

-(void)segElevatorControllAlertViewLayout{
    //高度固定
    _totalHeight = kElevatorControllAlertViewHeight;
    
    //高度不固定
    //_totalHeight = 0;
    _titleLabelHeight = 0;
    _titleLabelTextLayout = nil;
    _cancelLabelHeight = 0;
    _cancelLabelTextLayout = nil;
    
    if (!_data) return;
    
    [self layout];
    
    _totalHeight = 0;
    _totalHeight += kFloorHeadBottom * 2;
    NSInteger c = _data.dataSource.count >= 3 ? 3 : _data.dataSource.count;
    _totalHeight += c * kFloorHeight;
}

//数据处理
-(void)layout{
    [self segtitleLabelWithContent:@"选择楼层" fontSize:16 textColor:kColor_1];
    [self segcancelLabelWithContent:@"取消" fontSize:16 textColor:kColor_3];
}
//控件titleLabel文本布局计算
-(void)segtitleLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    //SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    //modifier.font = font;
    //modifier.paddingTop = 1;
    //modifier.paddingBottom = 1;
    //modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth, HUGE);
    //container.linePositionModifier = modifier;
    
    _titleLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    //if (!_titleLabelTextLayout)return;
    //_titleLabelHeight = [modifier heightForLineCount:_titleLabelTextLayout.rowCount];
}

//控件cancelLabel文本布局计算
-(void)segcancelLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    //SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    //modifier.font = font;
    //modifier.paddingTop = 1;
    //modifier.paddingBottom = 1;
    //modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth, HUGE);
    //container.linePositionModifier = modifier;
    
    _cancelLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    //if (!_cancelLabelTextLayout)return;
    //_cancelLabelHeight = [modifier heightForLineCount:_cancelLabelTextLayout.rowCount];
}

@end

/**************************SEGElevatorControllAlertView************************/
@implementation SEGElevatorControllAlertView
-(void)showWithData:(NSArray<SEGElevatorControllAlertFloorModel *> *)dataSource {
    
    UIView* _maskView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight())];
    _maskView.tag = 141503417;
    _maskView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    
    
    SEGElevatorControllAlertViewModel *data = [[SEGElevatorControllAlertViewModel alloc]init];
    data.widgetWidth = LYScreenWidth() - 50 * 2 * kKitScaleHeight;
    data.dataSource = dataSource;
    // 控件内容布局计算
    SEGElevatorControllAlertViewLayout *layout = [[SEGElevatorControllAlertViewLayout alloc]initWithData:data type:SEGEnumElevatorControllAlertViewType_NO];
    // 内容布局填充到视图
    
    [self setWithLayout:layout];
    [_maskView addSubview:self];
    [[UIApplication sharedApplication].keyWindow addSubview:_maskView];
    self.left = 50 * kKitScaleHeight ;
    self.top = (LYScreenHeight() - layout.totalHeight)*0.5;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kElevatorControllAlertViewHeight;
    }
    self = [super initWithFrame:frame];
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.cancelLabel];
    [self addSubview:self.backgroundView];
    
    if (kDebug)self.backgroundColor = kSegRandomColor;
    return self;
}

-(void)setWithLayout:(SEGElevatorControllAlertViewLayout *)layout{
    self.height = layout.totalHeight;
    self.width = layout.data.widgetWidth;
    _layout = layout;
    
    if (self.height <= 0) return;
    
    self.titleLabel.textLayout = layout.titleLabelTextLayout;
    self.cancelLabel.textLayout = layout.cancelLabelTextLayout;
    
    [self setupUI];
    
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 5;
    
    
    [self.backgroundView removeAllSubviews];
    __block CGFloat H = 0;

    [self.layout.data.dataSource enumerateObjectsUsingBlock:^(SEGElevatorControllAlertFloorModel*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        SEGPickViewModel *data = [[SEGPickViewModel alloc]init];
        data.content = obj.name;
        data.businessData = obj.businessData;
        data.widgetWidth = _layout.data.widgetWidth;
        
        SEGPickViewLayout *layout = [[SEGPickViewLayout alloc]initWithData:data type:SEGEnumPickViewType_NO];
        SEGPickView *varPickView = [[SEGPickView alloc]initWithFrame:CGRectZero];
        varPickView.left = 0;
        varPickView.top = idx * layout.totalHeight;
        [varPickView setWithLayout:layout];
        
        varPickView.layer.borderWidth = 0.25;
        varPickView.layer.borderColor = kColor_L2.CGColor;
        
        [self.backgroundView addSubview:varPickView];
        H += layout.totalHeight;
        
        varPickView.userInteractionEnabled = YES;
        __weak typeof(self) _self = self;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
            if (_self.delegate && [_self.delegate respondsToSelector:@selector(SEGprotocolDidSelectedRowData:elevId:elevType:keyU:)]) {
                [_self.delegate SEGprotocolDidSelectedRowData:varPickView.layout.data.businessData elevId:self.elevId elevType:self.elevType keyU:self.keyU];
                [self segRemove];
            }
        }];
        [varPickView addGestureRecognizer:tap];
    }];
    
    self.backgroundView.contentSize = CGSizeMake(_layout.data.widgetWidth,H);
}

//界面布局
-(void)setupUI{
    self.titleLabel.backgroundColor = kColor_W;
    self.cancelLabel.backgroundColor = kColor_W;
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.height.mas_equalTo(kFloorHeadBottom);
    }];
    
    [self.backgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(_layout.totalHeight - 2*kFloorHeadBottom);
        make.width.mas_equalTo(_layout.data.widgetWidth);
    }];
    
    [self.cancelLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backgroundView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(kFloorHeadBottom);
    }];
    
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    
    self.cancelLabel.textAlignment = NSTextAlignmentCenter;
    self.cancelLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
}

#pragma mark - getter
-(SEGLabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[SEGLabel alloc]init];
        _titleLabel.userInteractionEnabled = NO;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _titleLabel.displaysAsynchronously = YES;
        //_titleLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _titleLabel.fadeOnHighlight = NO;
        _titleLabel.numberOfLines = 0;
        _titleLabel.fadeOnAsynchronouslyDisplay = NO;
        _titleLabel.fadeOnAsynchronouslyDisplay = NO;
        _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        if (kDebug)_titleLabel.backgroundColor = kSegRandomColor;
        //        __weak typeof(self) _self = self;
        //        _titleLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        //            [SEGAlertView showTmepMessageWithMessage:@"_titleLabelClick"];
        //            if (_self.delegate && [_self.delegate respondsToSelector:@selector(SEGprotocoltitleLabel:)]) {
        //                [_self.delegate SEGprotocoltitleLabel:_self.titleLabel];
        //            }
        //        };
    }
    return _titleLabel;
}

-(SEGLabel *)cancelLabel{
    if (!_cancelLabel) {
        _cancelLabel = [[SEGLabel alloc]init];
        //_cancelLabel.userInteractionEnabled = NO;
        _cancelLabel.textAlignment = NSTextAlignmentCenter;
        _cancelLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _cancelLabel.displaysAsynchronously = YES;
        //_cancelLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _cancelLabel.fadeOnHighlight = NO;
        _cancelLabel.numberOfLines = 0;
        _cancelLabel.fadeOnAsynchronouslyDisplay = NO;
        _cancelLabel.fadeOnAsynchronouslyDisplay = NO;
        _cancelLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        if (kDebug)_cancelLabel.backgroundColor = kSegRandomColor;
        _cancelLabel.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            [self segRemove];
        };
    }
    return _cancelLabel;
}

-(void)segRemove{
    for (UIView *object in [UIApplication sharedApplication].keyWindow.subviews) {
        if(object.tag == 141503417) [object removeFromSuperview];
    }
}

-(UIScrollView *)backgroundView{
    if (!_backgroundView) {
        _backgroundView = [[UIScrollView alloc]init];
        _backgroundView.frame = self.frame;
        _backgroundView.backgroundColor = kColor_W;
        _backgroundView.contentSize = CGSizeMake(0, 0);
        _backgroundView.showsVerticalScrollIndicator = NO;
        _backgroundView.showsHorizontalScrollIndicator = NO;
        if (kDebug)_backgroundView.backgroundColor = kSegRandomColor;
    }
    return _backgroundView;
}



- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return kFloorHeight;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _layout.data.dataSource.count;
}
//指定每行如何展示数据（此处和tableview类似）
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return _layout.data.dataSource[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (self.delegate && [self.delegate respondsToSelector:@selector(SEGprotocolDidSelectedRowData:elevId:elevType:keyU:)]) {
        [self.delegate SEGprotocolDidSelectedRowData:_layout.data.dataSource[row] elevId:self.elevId elevType:self.elevType keyU:self.keyU];
    }
}
@end

